//
//  VisitorViewController.swift
//  ShangHaiWeiBo
//
//  Created by 蔡杰 on 16/2/25.
//  Copyright © 2016年 蔡杰. All rights reserved.
//

import UIKit
import SnapKit
//protocol VisitorViewDelegate: NSObjectProtocol {
//    
//    /// 访客视图注册
//    func visitorViewDidRegister()
//    /// 访客视图登录
//    func visitorViewDidLogin()
//}
protocol VisitorViewDelegate: NSObjectProtocol{


    /**
    访客视图注册
    */
    func visitorViewDidRegister()
    /**
    访客视图登陆
    */
    func visitorViewDidLogin()
}

class VisitorViewController: UIView {
    

    //设置代理属性
    weak var delegate:VisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setVisitorUI()
        landingButton.addTarget(self, action: "didLanding", forControlEvents: .TouchUpInside)
        registerButton.addTarget(self, action: "didRegister", forControlEvents: .TouchUpInside)
        
    }

//    weak var delegate : VisitorViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpInfo (imageName : String? = nil ,messageImageName: String){
    
    
        textLabel.text = messageImageName
        
        guard let imageName = imageName else {
        
            startAnimation()
            return
        }
        homeView.hidden = true
        maskRimView.hidden = true
        
        rimView.image = UIImage(named: imageName)
        
        
    }
    
    private func startAnimation(){
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        rimView.layer.addAnimation(anim, forKey: nil)
    }
    
    private lazy var rimView: UIImageView = UIImageView(cj_image: "visitordiscover_feed_image_smallicon")
    private lazy var maskRimView: UIImageView = UIImageView(cj_image: "visitordiscover_feed_mask_smallicon")
    private lazy var homeView: UIImageView = UIImageView(cj_image: "visitordiscover_feed_image_house")
    private lazy var textLabel: UILabel = UILabel(cj_text: "关注一些人，回这里看看有什么惊喜", alignment: .Center)
    //创建登陆按钮
    private lazy var landingButton: UIButton = UIButton(buttonName: "登陆", buttonColor: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    //创建注册按钮
    private lazy var registerButton: UIButton = UIButton(buttonName: "注册", buttonColor: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    
  
    @objc private func didLanding (){
        
        delegate?.visitorViewDidLogin()
        print("登陆")
    }
    
    @objc private func didRegister (){
        
        delegate?.visitorViewDidRegister()
        print("注册")
    }

    
 

}

    //MARK: 设置访客视图界面
extension VisitorViewController {


    private func setVisitorUI (){
    
        //添加控件
        addSubview(rimView)
        addSubview(maskRimView)
        addSubview(homeView)
        addSubview(textLabel)
        addSubview(landingButton)
        addSubview(registerButton)
        
               
        
        
     
        //自动布局
//        rimView.translatesAutoresizingMaskIntoConstraints = false
//        homeView.translatesAutoresizingMaskIntoConstraints = false
//        textLabel.translatesAutoresizingMaskIntoConstraints = false
        //下面的代码等于上面的代码
        for i in subviews {
        
            i.translatesAutoresizingMaskIntoConstraints = false
        }
        //注释的是Swift的原生布局框架

        //下面写的是SnapKit框架
        //圆圈的视图位置
//        self.addConstraint(NSLayoutConstraint(
//            item: rimView,
//            attribute: .CenterX,
//            relatedBy: .Equal,
//            toItem: self,
//            attribute: .CenterX,
//            multiplier: 1.0,
//            constant: 0))
//        self.addConstraint(NSLayoutConstraint(
//            item: rimView,
//            attribute: .CenterY,
//            relatedBy: .Equal,
//            toItem: self,
//            attribute: .CenterY,
//            multiplier: 1.0,
//            constant: -60))
        
        rimView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        //房子的视图位置
//        self.addConstraint(NSLayoutConstraint(
//            item: homeView,
//            attribute: .CenterX,
//            relatedBy: .Equal,
//            toItem: self,
//            attribute: .CenterX,
//            multiplier: 1.0,
//            constant: 0))
//        self.addConstraint(NSLayoutConstraint(
//            item: homeView,
//            attribute: .CenterY,
//            relatedBy: .Equal,
//            toItem: self,
//            attribute: .CenterY,
//            multiplier: 1.0,
//            constant: -60))
        homeView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        //提示文字的位置
//        self.addConstraint(NSLayoutConstraint(
//            item: textLabel,
//            attribute: .CenterX,
//            relatedBy: .Equal,
//            toItem: rimView,
//            attribute: .CenterX,
//            multiplier: 1.0,
//            constant: 0))
//        self.addConstraint(NSLayoutConstraint(
//            item: textLabel,
//            attribute: .Top,
//            relatedBy: .Equal,
//            toItem: rimView,
//            attribute: .Bottom,
//            multiplier: 1.0,
//            constant: 20))
        
        //指定高度和宽带
//        self.addConstraint(NSLayoutConstraint(
//            item: textLabel,
//            attribute: .Width,
//            relatedBy: .Equal,
//            toItem: nil,
//            attribute: NSLayoutAttribute.NotAnAttribute,
//            multiplier: 1.0,
//            constant: 224))
//        self.addConstraint(NSLayoutConstraint(
//            item: textLabel,
//            attribute: .Height,
//            relatedBy: .Equal,
//            toItem: nil,
//            attribute: NSLayoutAttribute.NotAnAttribute,
//            multiplier: 1.0,
//            constant: 36))
        textLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(rimView)
            make.top.equalTo(rimView.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 224, height: 36))
            
        }
        

        landingButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(20)
            make.right.equalTo(textLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(landingButton)
            make.left.equalTo(textLabel)
            make.size.equalTo(landingButton)
        }
        //设置遮盖视图
        maskRimView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.bottom.equalTo(landingButton)
        }
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    }
    
}